package cn.mothin.action.system;

/**
 * @author 宫晓鹏
 */
import org.springframework.beans.factory.annotation.Autowired;

import cn.mothin.frame.core.annotation.Module;
import cn.mothin.frame.core.annotation.NeedLogon;
import cn.mothin.frame.core.annotation.NeedModule;
import cn.mothin.frame.core.annotation.NeedPermission;
import cn.mothin.frame.core.annotation.NeedPermissionAs;
import cn.mothin.frame.core.annotation.NeedPermissionTemplate;
import cn.mothin.frame.core.annotation.OperationLink;
import cn.mothin.frame.core.annotation.OperationLinks;
import cn.mothin.frame.core.base.BaseAction;
import cn.mothin.service.system.OrganizationService;
import cn.mothin.vo.system.OrganizationForm;

@SuppressWarnings("serial")
@Module(id = "540e6e05a62120babb8094bc", name = "机构管理")
public class OrganizationAction extends BaseAction {
    private OrganizationForm organizationForm = new OrganizationForm();

    @Autowired
    private OrganizationService organizationService;

    @Override
    public Object getModel() {
        return organizationForm;
    }

    @NeedModule
    public String execute() {
        return SUCCESS;
    }

    @NeedModule
    public String queryOrganization() {
        return renderJson(toJson(organizationService.queryOrganization(organizationForm)));
    }

    /**
     * 查看置顶的机构信息
     * 
     * @return
     */
    @NeedModule
    public String view() {
        organizationService.query(organizationForm);
        return "view";
    }

    /**
     * 添加
     * 
     * @return
     */
    @OperationLink(link = "execute")
    @NeedPermissionTemplate(id = "540e92daa62120babb8094bd", template = NeedPermissionTemplate.CREATE)
    public String addOrganization() {
        return "add";
    }

    /**
     * 保存添加
     * 
     * @return
     */
    @NeedPermissionAs("addOrganization")
    public String saveForAddOrganization() {
        return renderJson(organizationService.addOrganization(organizationForm)
                .toJson());
    }

    /**
     * 添加子机构
     * 
     * @return
     */
    @OperationLinks({ @OperationLink(link = "view", ordinal = 0),
            @OperationLink(link = "execute", ordinal = 0, rowOperation = true) })
    @NeedPermissionTemplate(id = "54101b8ca621423ce1393a56", template = NeedPermissionTemplate.CREATE)
    public String addOrganizationchildren() {
        return "add";
    }

    /**
     * 保存添加子机构
     * 
     * @return
     */
    @NeedPermissionAs("addOrganization")
    public String saveForAddOrganizationchildren() {
        return renderJson(organizationService.addOrganization(organizationForm)
                .toJson());
    }

    /**
     * 修改
     * 
     * @return
     */
    @OperationLinks({ @OperationLink(link = "view", ordinal = 1),
            @OperationLink(link = "execute", ordinal = 1, rowOperation = true) })
    @NeedPermissionTemplate(id = "540e954ba62120babb8094be", template = NeedPermissionTemplate.UPDATE)
    public String updateOrganization() {
        if (organizationService.queryOrganizationForUpdate(organizationForm)
                .isOk())
            return "update";
        else {
            return ERROR;
        }
    }

    /**
     * 保存修改
     * 
     * @return
     */
    @NeedPermissionAs("updateOrganization")
    public String saveForUpdateOrganization() {
        return renderJson(organizationService.updateOrganization(
                organizationForm).toJson());
    }

    /**
     * 删除
     * 
     * @return
     */
    @OperationLinks({ @OperationLink(link = "view", ordinal = 2),
            @OperationLink(link = "execute", ordinal = 2, rowOperation = true) })
    @NeedPermissionTemplate(id = "540e98ffa62120babb8094bf", template = NeedPermissionTemplate.DELETE)
    public String deleteOrganization() {
        return renderJson(organizationService.deleteOrganization(
                organizationForm).toJson());
    }

    /**
     * 设置部门负责人
     * 
     * @return
     */
    @OperationLinks({ @OperationLink(link = "view", ordinal = 3),
            @OperationLink(link = "execute", ordinal = 3, rowOperation = true) })
    @NeedPermission(id = "540e9f80a62120babb8094c0", name = "设置部门负责人")
    public String showManager() {
        return "select_manager";
    }

    /**
     * 选择部门负责人
     * 
     * @return
     */
    @NeedPermissionAs("showManager")
    public String selectManager() {
        return renderJson(organizationService.selectManager(organizationForm));
    }

    /**
     * 保存部门负责人
     * 
     * @return
     */
    @NeedPermissionAs("showManager")
    public String saveForSelectManager() {
        return renderJson(organizationService.updateManager(organizationForm)
                .toJson());
    }

    @NeedLogon
    public String findOrganizations() {
        return renderJson(organizationService.deleteOrganization(
                organizationForm).toJson());
    }

    @NeedLogon
    public String findOrganizationById() {
        return renderJson(organizationService.deleteOrganization(
                organizationForm).toJson());
    }

    /**
     * 控件数据
     * 
     * @return
     */
    @NeedLogon
    public String queryControlOrganization() {
        return renderJson(toJson(organizationService.queryControlOrganization(organizationForm)));
    }

}
